﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyAnimelistRecommender.DataObject;

namespace MyAnimelistRecommender.View
{
    /// <summary>
    /// Interaktionslogik für MetroWindow.xaml
    /// </summary>
    public partial class MetroWindow : Window
    {
        public MetroWindow()
        {
            try
            {
                if (Properties.Settings.Default.SaveState != null)
                {
                    App.Model = Properties.Settings.Default.SaveState.Model;
                    DataHelper.RecommendationDataHelper.LoadData(this, App.Model, true);
                }
            }
            catch (Exception) { }
            this.DataContext = App.Model;

            this.Closing += (s, e) =>
            {
                e.Cancel = true;
                this.Hide();
                new Thread(() =>
                {
                    DataHelper.RecommendationDataHelper.Save(App.Model);
                    Dispatcher.InvokeShutdown();
                }).Start();
            };

            InitializeComponent();
        }

        private void animeHeadlineTextblock_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (listBox.SelectedItems.Count == listBox.Items.Count)
                listBox.SelectedIndex = -1;
            else
                listBox.SelectAll();
        }

        private void genrefilterGo_Click(object sender, RoutedEventArgs e)
        {
            DataHelper.RecommendationDataHelper.FilterByGenres();
        }

        private void genreFilterToggle_Click(object sender, RoutedEventArgs e)
        {
            if (genreFilterStack.IsVisible)
                genreFilterStack.Visibility = System.Windows.Visibility.Collapsed;
            else
                genreFilterStack.Visibility = System.Windows.Visibility.Visible;
        }

        private bool IsText(string text)
        {
            string eh = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            return eh.Contains(text);
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBox.SelectedIndex == -1)
                return;

            App.Model.AnimeList.Select(x => x).Where(x => listBox.SelectedItems.Contains(x)).ToList().ForEach(x => x.IsActivated = true);
            App.Model.AnimeList.Select(x => x).Where(x => !listBox.SelectedItems.Contains(x)).ToList().ForEach(x => x.IsActivated = false);

            DataHelper.RecommendationDataHelper.CalcBestAnime(App.Model);
        }

        private void listBox2_KeyUp(object sender, KeyEventArgs e)
        {
            var k = e.Key.ToString();
            if (IsText(e.Key.ToString()) && searchTextBox.Visibility == System.Windows.Visibility.Collapsed)
            {
                searchTextBox.Visibility = System.Windows.Visibility.Visible;
                searchTextBox.Focus();
                searchTextBox.Text = e.Key.ToString();
                searchTextBox.Select(searchTextBox.Text.Length, 0);
            }
        }

        private void reloadButton_Click(object sender, RoutedEventArgs e)
        {
            DataHelper.RecommendationDataHelper.LoadData(this, App.Model);
        }

        private void searchTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                searchTextBox.Visibility = System.Windows.Visibility.Collapsed;
                App.Model.SearchText = "";
                e.Handled = true;
            }
        }

        private void SortingModeButton_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < App.Model.SortingMode.Count; i++)
            {
                if (App.Model.SortingMode.Keys.ElementAt(i) == App.Model.SelectedSortingMode)
                {
                    if (i + 1 < App.Model.SortingMode.Count)
                        App.Model.SelectedSortingMode = App.Model.SortingMode.Keys.ElementAt(i + 1);
                    else
                        App.Model.SelectedSortingMode = App.Model.SortingMode.Keys.ElementAt(0);
                    break;
                }
            }
        }

        private void myanimelistButton_Click(object sender, RoutedEventArgs e)
        {
            string uri = (string)((Button)sender).Tag;
            if (Uri.IsWellFormedUriString(uri, UriKind.Absolute))
            {
                Process.Start(uri);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }
}